﻿using UnityEngine;
using System.Collections;
using FrenzyIsland.UI;
using Game.Scripts.Managers;

[RequireComponent(typeof(SelectedBuildingModel))]
public class SelectedBuildingController : MonoBehaviour {

    public UIMessageBox uiMessageBoxDeleteBuilding;
    public UIMessageBox uiMessageBoxLevelUP;
    public GameObject workersCompositionWidget;

    // Use this for initialization
    void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void btnQuit_Click()
    {
        GuiManager gui = UIRoot.list[0].GetComponent<GuiManager>();
        gui.CloseWidget(this.gameObject);
    }

    public void btnLevelUP_Click()
    {
        GuiManager gui = UIRoot.list[0].GetComponent<GuiManager>();

        UIMessageBoxBuildingLevelUp msg = (UIMessageBoxBuildingLevelUp)gui.showMessageBox(GetComponent<UIPanel>(), uiMessageBoxLevelUP,
            new GuiManager.SimpleDelegate(btnLevelUP_Click_OK),
            new GuiManager.SimpleDelegate(btnLevelUP_Click_Cancel));

        msg.building = GetComponent<SelectedBuildingModel>().building;
    }

    private void btnLevelUP_Click_OK()
    {
        Building building = GetComponent<SelectedBuildingModel>().building;
        building.levelUP();
    }

    private void btnLevelUP_Click_Cancel()
    {

    }

    public void btnShowWorkers_Click(){
        GuiManager gui = UIRoot.list[0].GetComponent<GuiManager>();
        if (this.workersCompositionWidget != null) {
            UIRoot root = UIRoot.list[0];
            UIPopup popupRoot = this.GetComponent<UIPopup>();
            GuiManager guiManager = root.GetComponent<GuiManager>();
            GameObject popup = guiManager.openPopup(popupRoot, this.workersCompositionWidget);
            
            SelectedBuildingModel popupModel = popup.GetComponent<SelectedBuildingModel>();
            if (popupModel != null) {
                SelectedBuildingModel model = this.GetComponent<SelectedBuildingModel>();
                popupModel.building = model.building;
            }
        }
    }

    public void btnDelete_click()
    {
        GuiManager gui = UIRoot.list[0].GetComponent<GuiManager>();

        UIMessageBoxDeleteBuilding msg = (UIMessageBoxDeleteBuilding)gui.showMessageBox(GetComponent<UIPanel>(), uiMessageBoxDeleteBuilding, 
            new GuiManager.SimpleDelegate(btnDelete_Click_OK),
            new GuiManager.SimpleDelegate(btnDelete_Click_Cancel));
        msg.building = GetComponent<SelectedBuildingModel>().building;
    }

    private void btnDelete_Click_OK()
    {
        //cancello i riferimenti al building
        Building building = GetComponent<SelectedBuildingModel>().building;
        building.prototype.RemoveBuilding(building);
        
        //chiudo il popup
        GuiManager gui = UIRoot.list[0].GetComponent<GuiManager>();
        gui.CloseWidget(this.gameObject);


    }

    private void btnDelete_Click_Cancel()
    {

    }
}
